
package com.tenentia.pcmps.swm.web.jsf.beans;

import javax.faces.context.FacesContext;
import javax.portlet.PortletRequest;

import org.apache.log4j.Logger;

import com.liferay.portal.model.User;
import com.liferay.portal.util.PortalUtil;
import com.tenentia.pcmps.swm.business.BusinessLogicObjectsFactory;
import com.tenentia.pcmps.swm.business.interfaces.UsersManager;
import com.tenentia.pcmps.swm.data.beans.ApplicationUser;
import com.tenentia.pcmps.swm.web.jsf.util.JSFUtilities;

/**
 * 
 * @author tEnEntia, S.L.N.E.
 * 
 */
public class ApplicationAuthenticatedUser {

	private static final Logger LOGGER = Logger.getLogger(ApplicationAuthenticatedUser.class.getPackage().getName());

	private Boolean insurancePoliciesLinkRendered;

	private Boolean marketingPromotionsLinkRendered;

	private Boolean templatesLinkRendered;

	private Boolean usersLinkRendered;

	private User authenticatedUser;

	private ApplicationUser applicationAuthenticatedUser;

	public ApplicationAuthenticatedUser() {

		super();

		insurancePoliciesLinkRendered = Boolean.FALSE;
		marketingPromotionsLinkRendered = Boolean.FALSE;
		templatesLinkRendered = Boolean.FALSE;
		usersLinkRendered = Boolean.FALSE;

		boolean isNullUser = true;

		boolean isNullRole = true;

		try {

			authenticatedUser = PortalUtil.getUser((PortletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest());

			LOGGER.debug("authenticatedUser: " + authenticatedUser);

			if (null != authenticatedUser) {

				isNullUser = false;

				UsersManager usersManager = (UsersManager) BusinessLogicObjectsFactory.getInstance().createNewBusinessObject(BusinessLogicObjectsFactory.USERS_MANAGER);
				applicationAuthenticatedUser = new ApplicationUser();
				applicationAuthenticatedUser.setInternalID(authenticatedUser.getUserId());
				applicationAuthenticatedUser = usersManager.get(applicationAuthenticatedUser);

				if (null != applicationAuthenticatedUser.getRole() && null != applicationAuthenticatedUser.getRole().getInternalID()) {

					isNullRole = false;

					insurancePoliciesLinkRendered = Boolean.TRUE;

					LOGGER.info("User logged as '" + applicationAuthenticatedUser.getRole().getName() + "'.");

					if (applicationAuthenticatedUser.getRole().getInternalID().equals(Long.valueOf(1))) {

						// Administrador

						marketingPromotionsLinkRendered = Boolean.TRUE;
						templatesLinkRendered = Boolean.TRUE;
						usersLinkRendered = Boolean.TRUE;

						insurancePoliciesLinkRendered = Boolean.FALSE;
					}
				}

			}

		} catch (Exception e) {
			LOGGER.error(e, e);
		}

		ApplicationConfiguration applicationConfiguration = (ApplicationConfiguration) JSFUtilities.getManagedObject("ApplicationConfiguration");

		if (applicationConfiguration.getDebugEnabled().booleanValue() && (isNullUser || isNullRole)) {

			LOGGER.info("Application running in DEBUG mode...");

			insurancePoliciesLinkRendered = Boolean.TRUE;
			marketingPromotionsLinkRendered = Boolean.TRUE;
			templatesLinkRendered = Boolean.TRUE;
			usersLinkRendered = Boolean.TRUE;
		}

	}

	public Boolean getInsurancePoliciesLinkRendered() {

		return insurancePoliciesLinkRendered;
	}

	public Boolean getMarketingPromotionsLinkRendered() {

		return marketingPromotionsLinkRendered;
	}

	public Boolean getTemplatesLinkRendered() {

		return templatesLinkRendered;
	}

	public Boolean getUsersLinkRendered() {

		return usersLinkRendered;
	}

	public User getAuthenticatedUser() {

		return authenticatedUser;
	}

	public ApplicationUser getApplicationAuthenticatedUser() {

		return applicationAuthenticatedUser;
	}

	public void setInsurancePoliciesLinkRendered(Boolean insurancePoliciesLinkRendered) {

		this.insurancePoliciesLinkRendered = insurancePoliciesLinkRendered;
	}

	public void setMarketingPromotionsLinkRendered(Boolean marketingPromotionsLinkRendered) {

		this.marketingPromotionsLinkRendered = marketingPromotionsLinkRendered;
	}

	public void setTemplatesLinkRendered(Boolean templatesLinkRendered) {

		this.templatesLinkRendered = templatesLinkRendered;
	}

	public void setUsersLinkRendered(Boolean usersLinkRendered) {

		this.usersLinkRendered = usersLinkRendered;
	}

	public void setAuthenticatedUser(User authenticatedUser) {

		this.authenticatedUser = authenticatedUser;
	}

	public void setApplicationAuthenticatedUser(ApplicationUser applicationAuthenticatedUser) {

		this.applicationAuthenticatedUser = applicationAuthenticatedUser;
	}

}
